package com.ruoyi.system.mapper.bm;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruoyi.system.domain.UpdatePaymentStatusAndPaymentDataReqDTO;
import com.ruoyi.system.domain.bm.BMCoach;
import com.ruoyi.system.domain.bm.BMPayment;
import com.ruoyi.system.domain.bm.dto.GetOrderStatusDTO;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;


/**
 */
public interface BMPaymentMapper extends BaseMapper<BMPayment>{


    default BMPayment isExistByOutOrderSn(String outOrderSn){
        LambdaQueryWrapper<BMPayment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BMPayment::getOutOrderSn, outOrderSn);
        queryWrapper.last("limit 1");
        return selectOne(queryWrapper);
    }

    default BMPayment updatePaymentStatusAndPaymentData(UpdatePaymentStatusAndPaymentDataReqDTO updatePaymentStatusAndPaymentDataReqDTO, int status){
        BMPayment update = new BMPayment();
        update.setId(updatePaymentStatusAndPaymentDataReqDTO.getId());
        update.setPaymentTime(updatePaymentStatusAndPaymentDataReqDTO.getPaidTime());
        update.setTransactionNo(updatePaymentStatusAndPaymentDataReqDTO.getTradeId());
        update.setPaymentStatus(status);
        updateById(update);
        return update;
    }

    default BMPayment selectByObjectId(Long objectId, Integer objectType){
        LambdaQueryWrapper<BMPayment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BMPayment::getObjectId, objectId);
        queryWrapper.eq(BMPayment::getObjectType, objectType);
        queryWrapper.last("limit 1");
        return selectOne(queryWrapper);
    }

    default List<BMPayment> selectByObjectIds(Integer type, Set<Long> ids){
        if (CollUtil.isEmpty(ids)) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<BMPayment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(BMPayment::getObjectId, ids);
        queryWrapper.eq(BMPayment::getObjectType, type);
        return selectList(queryWrapper);
    }

    default BMPayment updatePaymentStatusAndRefund(Long id, LocalDateTime paidTime, String transactionId, int status){
        BMPayment update = new BMPayment();
        update.setId(id);
        update.setRefundTime(paidTime);
        update.setRefundTradeId(transactionId);
        update.setPaymentStatus(status);
        updateById(update);
        return update;
    }
}

